package fragment.livefragments;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.LiveFmEntity;
import fragment.AbsBaseFragment;
import utils.adapter.LiveFmAdpter;
import volley.VolleySingleton;

/**
 * 直播页面对应的电台页面
 * Created by dllo on 15/11/20.
 */
public class LiveFmfragment extends AbsBaseFragment {
    private RecyclerView recyclerView;
    private TextView titleForAllTv;
    private LiveFmAdpter adpter;
    String URL ,title;
    private ImageView back;


    @Override
    protected int getLayoutView() {
        return R.layout.fragment_livefm;
    }

    @Override
    protected void initView() {
        recyclerView = findView(R.id.livefm_recylerView);
        GridLayoutManager manager = new GridLayoutManager(getContext(),1);
        recyclerView.setLayoutManager(manager);
        titleForAllTv = findView(R.id.titleForAllTv);
        adpter = new LiveFmAdpter();
        recyclerView.setAdapter(adpter);
        back = findView(R.id.titleForAllBack);




    }

    //静态方法得到数据
    public static LiveFmfragment newInstance(String URL,String title) {
        LiveFmfragment fragment = new LiveFmfragment();
        Bundle args = new Bundle();
        args.putString("URL", URL);
        args.putString("title",title);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initData() {

        if (getArguments() != null) {
            URL = getArguments().getString("URL");
            title = getArguments().getString("title");
            titleForAllTv.setText(title);
        }
        initNetwork();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
    }

    private void initNetwork() {
        VolleySingleton.getInstance().addRequest(URL,
                LiveFmEntity.class, new Response.Listener<LiveFmEntity>() {
                    @Override
                    public void onResponse(LiveFmEntity response) {
                        Log.d("CollectionPageFragment", "response:" + response);
                        if (response != null) {
                           List<LiveFmEntity.ResultEntity> result = response.getResult();
                            adpter.add(result);
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }

}
